<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 放置页签 -->
          <el-tab-pane label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="height:60px">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
                @click="hAdd"
              >新增角色</el-button>
            </el-row>
            <!-- 表格 -->
            <el-table :data="list">
              <el-table-column label="序号" width="120" type="index" />
              <el-table-column label="角色名称" width="240" prop="name" />
              <el-table-column label="描述" prop="description" />
              <el-table-column label="操作">
                <template #default="{row}">
                  <el-button size="small" type="success">分配权限</el-button>
                  <el-button size="small" type="primary" @click="hEdit(row)">编辑</el-button>
                  <el-button size="small" type="danger">删除</el-button>
                </template>
              </el-table-column>
            </el-table>

            <el-row type="flex" justify="center" align="middle" style="height: 60px">
              <!-- 分页组件 -->
              <el-pagination
                :current-page="queryParams.page"
                :page-sizes="[2,3,5,10,20]"
                :page-size="queryParams.pagesize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total"
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
              />
            </el-row>
          </el-tab-pane>
        </el-tabs>
      </el-card>
      <!-- 新增角色的对话框 -->
      <el-dialog title="新增角色" :visible.sync="showDialog">
        <el-form ref="roleForm" :model="roleForm" :rules="rules" label-width="100px">
          <el-form-item label="角色名称" prop="name">
            <el-input v-model="roleForm.name" />
          </el-form-item>
          <el-form-item label="角色描述" prop="description">
            <el-input v-model="roleForm.description" />
          </el-form-item>
        </el-form>
        <!-- 底部 -->
        <el-row slot="footer" type="flex" justify="center">
          <el-col :span="6">
            <el-button size="small">取消</el-button>
            <el-button size="small" type="primary" @click="hSubmit">确定</el-button>
          </el-col>
        </el-row></el-dialog>
    </div>
  </div>
</template>

<script>
import { getRolesList, addRole, updateRole } from '@/api/setting'
export default {
  data() {
    return {
      isEdit: false,
      roleForm: {
        name: '',
        description: ''
      },
      rules: {
        name: [{ required: true, message: '角色名称不能为空', trigger: 'blur' }],
        description: [{ required: true, message: '角色描述不能为空', trigger: 'blur' }]
      },
      showDialog: false,
      queryParams: {
        page: 1,
        pagesize: 2
      },
      list: [], // 储存角色的列表
      total: 0
    }
  },
  created() {
    this.loadRolesList()
  },
  methods: {
    async doEdit() {
      try {
        await updateRole(this.roleForm)
        this.loadRolesList()
        this.showDialog = false
      } catch (error) {
        console.log(error)
      }
    },
    hAdd() {
      this.isEdit = false
      this.showDialog = true
    },
    hEdit(data) {
      this.isEdit = true
      this.roleForm = { ...data }
      this.showDialog = true
    },
    async doAdd() {
      try {
        const res = await addRole(this.roleForm)
        console.log(res, 'res')
        this.total++ // 只要新增成功,后台的总数就加1,所以在计算最大页码值时,应该先加.在计算
        // 将queryParams.page改变到最后一页在发送数据
        // 计算出最大的页码值
        const maxPage = Math.ceil(this.total / this.queryParams.pagesize)
        this.queryParams.pagesize = maxPage
        // 重新渲染数据
        this.loadRolesList()
        // 关闭弹层
        this.showDialog = false
      } catch (error) {
        console.log(error)
      }
    },
    hSubmit() {
      this.$refs.roleForm.validate(valid => {
        if (!valid) return // 校验失败
        this.isEdit ? this.doEdit() : this.doAdd()
      })
    },
    // 当每页显示条数进行切换时触发,val=>每页显示的条数
    handleSizeChange(val) {
      console.log(`每页${val}条`)
      // 改变queryParams.pagesize每页显示的条数
      this.queryParams.page = 1
      this.queryParams.pagesize = val
      // 重新发送请求,重新获取数据
      this.loadRolesList()
    },
    // 当当前的页码值进行切换时触发
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`)
      // 改变queryParams.page页码值
      this.queryParams.page = val
      this.loadRolesList()
    },
    // 加载角色列表
    async loadRolesList() {
      try {
        const res = await getRolesList(this.queryParams)
        console.log(res, 'res11111111111')
        this.list = res.data.rows
        this.total = res.data.total
      } catch (error) {
        console.log(error, 'error')
      }
    }
  }
}
</script>

<style>

</style>
